
const { dataSource } = this.widgetObj._option
const params = { ...$event }

console.log('dataSource: => ', dataSource)
console.log('单击', params)

const {
    proxy,
    equipmentSearch,
    baseRequest,
    handleEquipmentData,
    renderTree,
    queryAreaData,
    queryQuotaByArea,
    queryFuseData,
    queryMaintenanceData,
} = window._videoAccess;
const { id, show, isFetch, projectCode, type } = params;

// function handleRows(data, rows) {
//     return data.map(item => {
//         if (item.id === id) return { ...item, isFetch: true, children: rows };
//         if (item.children && item.children.length) {
//             return { ...item, children: handleRows(item.children, rows) }
//         }
//         return item;
//     })
// }

// const renderTree = (rows) => {
//     const data = handleRows(dataSource, rows);
//     this.widgetObj.setData([{ data }]);
//     this.widgetObj.render();
// }

async function handleAreaData() {
    const { areaRows, equipmentRows, fusesRows } = await queryQuotaByArea(id, projectCode);
    renderTree([...areaRows, ...equipmentRows, ...fusesRows], id);
}

async function handleFuseData() {
    const rows = await queryFuseData(projectCode);
    renderTree(rows, id);
}

async function handleMaintenanceData() {
    console.log('查询维修中设备: => ', params)
    const rows = await queryMaintenanceData(projectCode);
    renderTree(rows, id);
}

// // 查询区域数据
// const queryAreaData = async () => {
//     const url = `/${proxy}Basics/basics-main/system/equipmentObject/selectListByParentId`;
//     const loading = layer.load(0);
//     const res = await baseRequest(url, {
//         parentProjectAreaId: id,
//         equipmentTypeCode: 'camera',
//         equipmentName: equipmentSearch,
//     }, 'POST');
//     layer.close(loading);
//     const arearows = handleAreaData(res?.result?.areaList, projectCode);
//     const equipmentRows = (res?.result?.equipmentList || []).map(item => handleEquipmentData(item));
//     renderTree([...arearows, ...equipmentRows]);
// }

// // 查询融合视频数据
// const queryFuseData = async () => {
//     const url = `/${proxy}SummaryAssessmentSV/videoFuse/query`;
//     const loading = layer.load(0);
//     const res = await baseRequest(url, { projectCode, fuseName: equipmentSearch }, 'POST');
//     layer.close(loading);
//     const rows = (res?.result || []).map(item => ({
//         ...item,
//         title: item.fuseName,
//         type: 'fuse',
//     }));
//     renderTree(rows);
// }

// // 查询维修中设备
// const queryMaintenanceData = async () => {
//     const url = `/${proxy}Basics/basics-main/system/equipmentObject/listRepairingEquipment`;
//     const loading = layer.load(0);
//     const res = await baseRequest(`${url}?projectCode=${projectCode}&equipmentName=${equipmentSearch}&equipmentTypeCode=camera`);
//     layer.close(loading);
//     const rows = (res?.result?.rows || []).map(item => ({ ...handleEquipmentData(item), type: 'maintenance' }));
//     renderTree(rows);
// }

if (show && !isFetch) {
    if (type === 'fuse-video') {
        // 融合视频
        handleFuseData();
    } else if (type === 'maintenance-video') {
        // 维修中设备
        handleMaintenanceData();
    } else {
        // 区域设备
        handleAreaData();
    }
}


// if (show && !isFetch) {
//     if (type === 'fuse-video') {
//         queryFuseData(projectCode);
//     } else if (type === 'maintenance-video') {
//         handleMaintenanceData(projectCode);
//     } else {
//         queryAreaData(id, projectCode);
//     }
// }
